package com.ruoyi.students.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.students.domain.Awards;
import com.ruoyi.students.domain.Students;
import com.ruoyi.students.service.IAwardsService;
import com.ruoyi.students.service.IStudentsService;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获奖信息Controller
 *
 * @author yxy
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/awards/awards")
public class AwardsController extends BaseController {
    @Autowired
    private IAwardsService awardsService;
    @Autowired
    private IStudentsService studentsService;
    @Autowired
    private ITeacherService teacherService;

    /**
     * 查询获奖信息列表
     */
    @PreAuthorize("@ss.hasPermi('awards:awards:list')")
    @GetMapping("/list")
    public TableDataInfo list(Awards awards) {
        startPage();
        List<Awards> list = awardsService.selectAwardsList(awards);
        return getDataTable(list);
    }

    /**
     * 根据学号获取信息
     */

    @Anonymous
    @GetMapping(value = "/getaward/{studentId}")
    public AjaxResult getStudentInfo(@PathVariable("studentId") String studentId) {
         List<Awards> awardsList = awardsService.selectStudentsByStudentId(studentId);
        return AjaxResult.success(awardsList);
    }
    /**
     * 导出获奖信息列表
     */
    @PreAuthorize("@ss.hasPermi('awards:awards:export')")
    @Log(title = "获奖信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Awards awards) {
        List<Awards> list = awardsService.selectAwardsList(awards);
        ExcelUtil<Awards> util = new ExcelUtil<Awards>(Awards.class);
        util.exportExcel(response, list, "获奖信息数据");
    }

    /**
     * 根据awardId获取得奖的详细信息
     */
    @Anonymous
    @GetMapping(value = "/{awardId}")
    public AjaxResult getInfo(@PathVariable("awardId") String awardId) {
        Awards list = awardsService.selectAwardsByAwardId(awardId);
        return AjaxResult.success(list);
    }

    /**
     * 新增获奖信息
     */
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody Awards awards) {
        return toAjax(awardsService.insertAwards(awards));
    }

    /**
     * 修改获奖信息
     */
    @Anonymous
    @PutMapping
    public AjaxResult edit(@RequestBody Awards awards) {
        return toAjax(awardsService.updateAwards(awards));
    }

    /**
     * 删除获奖信息
     */
    @Anonymous
    @DeleteMapping("/{awardIds}")
    public AjaxResult remove(@PathVariable Long[] awardIds) {
        return toAjax(awardsService.deleteAwardsByAwardIds(awardIds));
    }

    /**
     * 根据老师id获取下面的学生 , 然后根据学生学号获取获奖信息返回
     */
    @Anonymous
    @GetMapping(value = "/listByTeacherId/{teacherId}")
    public List<Awards> listByTeacher(@PathVariable("teacherId") String teacherId) {
        // 根据教师id查询老师信息
        Teacher teacher = new Teacher();
        teacher.setTeacherid(teacherId);
        List<Teacher> teacherlist = teacherService.selectTeacherList(teacher);
        Teacher Thisteacher = teacherlist.get(0);
        // 查询学生
        Students students = new Students();
        students.setStudentclass(Thisteacher.getClassss());
        students.setYearAdmission(Thisteacher.getYearAdmission());
        students.setScchoolMeaasge(Thisteacher.getSchool());
        List<Students> studentlist = studentsService.selectStudentsList(students);

        // 遍历学生列表，获取学生学号，然后根据学号查询获奖信息
        List<Awards> awardslist = new ArrayList<>();
        for (Students student : studentlist) {
            String studentId = student.getStudentId();
            List<Awards> awards = awardsService.selectAwardsListByStudentId(studentId);
            awardslist.addAll(awards);
        }
        return awardslist;
    }

    /**
     * 在单个老师的情况下筛选数据
     */
    @Anonymous
    @GetMapping(value = "/listByFilter/{teacherId}")
    public AjaxResult listByFilter(@PathVariable("teacherId") String teacherId,
                                   @ModelAttribute Awards awards) {
        List<Awards> list = listByTeacher(teacherId);
        System.out.println(list);
        String awardLevel = awards.getAwardLevel() == null ? "" : awards.getAwardLevel();
        String studentId = awards.getStudentId() == null ? "" : awards.getStudentId();
        return AjaxResult.success(list.stream()
                .filter(item -> (awardLevel.isEmpty() || awardLevel.equals(item.getAwardLevel())))
                .filter(item -> (studentId.isEmpty() || studentId.equals(item.getStudentId())))
                .collect(Collectors.toList())); // 收集结果;
    }
}
